<template>
  <div>
    <SearchContainer @search="getData" @reset="() => formRef.resetFields()">
      <a-form ref="formRef" class="form-style-flex" :model="formState" :label-col="{ style: { width: '80px' } }">
        <a-form-item name="zjm" label="" class="w-1/5">
          <a-input v-model:value="formState.zjm" placeholder="处方单号/姓名/助记码" />
        </a-form-item>
        <a-form-item name="dateRange" label="发药日期">
          <a-range-picker v-model:value="formState.dateRange" format="YYYY-MM-DD" value-format="YYYY-MM-DD" />
        </a-form-item>
      </a-form>
    </SearchContainer>
    <a-card class="mt-2 flex-1">
      <BaseTable :loading="loading" column-code="000636" fill :data-source="tableData" bordered :pagination="pagination">
        <template #bodyCell="{ column, record }">
          <template v-if="column.dataIndex === 'cfbh'">
            <PopoverTable :config="detailConfig" :width="800" :params="{ cfbh: record.cfbh, jzbh: record.jzbh }">
              <template #bodyCell="{ column: popColumn, record: popRecord }">
                <template v-if="popColumn.dataIndex === 'mc'">
                  <MedicineInfo :value="{ mc: popRecord.mc, gg: popRecord.gg, cj: popRecord.cj }" />
                </template>
              </template>
              {{ record.cfbh }}
            </PopoverTable>
          </template>
          <template v-if="column.dataIndex == 'shsj'">
            {{ record.sj }}
          </template>
          <template v-if="column.dataIndex === 'action'">
            <a-button type="link" size="small" @click="onPrint(record)"> 打印处方单 </a-button>
          </template>
        </template>
      </BaseTable>
    </a-card>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import SearchContainer from '@/components/SearchContainer'

const formRef = ref(null)
const formState = ref({
  dateRange: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  operationmode: 4
})
const detailConfig = ref({
  url: 'outp/mzyf/getPatientPrescriptionDetailList',
  columnCode: '000637'
})
const onPrint = async (item) => {
  const { code, msg, data } = await post('/outp/outPrint/getMedicinePrescriptionPrint', { ...item, jklb: '2' })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
const { loading, tableData, pagination, getData } = useTable({
  url: '/outp/mzyf/getPatientPrescriptionList',
  params: formState.value,
  formatParams: (params) => {
    return {
      stdate: params['dateRange']?.[0],
      eddate: params['dateRange']?.[1],
      ...params
    }
  }
})

onMounted(() => {
  getData()
})
</script>
